#include "Bitstr.h"
#include <math.h>


/**
*	Omvandla.h is not a Class in itself, but rather a collection of regular functions to be aplied to the bitstr class.
*	The two functions is for converting back and forth between integer and bitstr
*/

void tal_str( int tal, Bitstr * &str )
{
	bool negative = false;

	if ( tal < 0 )
	{
		negative = true;
		tal = -tal;
	}

	while( tal > 1 )
		{
			if( tal % 2 == 0 )
				str->push_front( Bit('0') );
			else
				str->push_front( Bit('1') );
			tal = tal / 2;
		}
		if( tal == 1 )
			str->push_front( Bit('1') );
		else
			str->push_front( Bit('0') );   

	if( negative )
	{
		str = str->twocomp();
	}


}

int str_tal( Bitstr str )
{
	int bit = 0;
	int sum = 0;
	int dummy;
	while( !str.empty() )
	{
		Bit temp = str.back();
		if( temp.GetValue() == '1' )
			dummy = 1;
		else
			dummy = 0;

		sum += ( dummy * pow(2,bit) );
		bit++;

		str.pop_back();
	}

	return sum;
}